package com.hqyj.shop.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.hqyj.shop.pojo.MyQrCode;
import com.hqyj.shop.pojo.OrderInfo;
import com.hqyj.shop.service.OrderInfoService;
import com.hqyj.shop.util.ResultVo;
import com.hqyj.shop.websocket.AlipayWebsocket;
import com.hqyj.shop.websocket.DemoWebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author rock
 * @since 2024-10-16 10:45:08
 */
@RestController
@RequestMapping("/order-info")
@Slf4j
public class OrderInfoController {

    @Autowired
    OrderInfoService service;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    //手机支付成功需要访问的地址
    @PostMapping("/test")
    public void test(HttpServletRequest request){

        log.info("手机支付成功");
        //发送消息到客户端
        AlipayWebsocket.sendMsg("success");

        System.out.println("支付状态："+ request.getParameter("trade_status"));
        System.out.println("商品订单号："+ request.getParameter("out_trade_no"));
        System.out.println("subject："+ request.getParameter("subject"));

        service.updateOrder(request.getParameter("out_trade_no"),request.getParameter("subject"));
    }

    /**
     * 加了消息队列的处理订单
     * @param request
     */
    @PostMapping("/test01")
    public void test01(HttpServletRequest request){

        log.info("手机支付成功");
        //发送消息到客户端
        AlipayWebsocket.sendMsg("success");
        String str = request.getParameter("subject");
        String data[] = str.split("&");
        //发送消息队里
        Map<String,Object> map = new HashMap<>();
        map.put("no",request.getParameter("out_trade_no"));
        map.put("subject",str);
        redisTemplate.opsForList().leftPush("order",map);

    }



    //生成二维码
    @GetMapping("/creadeQrCode")
    public void creadeQrCode(HttpServletResponse response, MyQrCode code){

        try {
            //获取付款链接地址
            String  url = service.createQrCode(code);
            //设置编码
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            pw.print(url);
            pw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //订单生成
    @PostMapping("/createOrder")
    public ResultVo createOrder(@RequestBody OrderInfo orderInfo,HttpServletRequest request){
        return service.insert(orderInfo,request);
    }

}

